package butines.kraz.entity;




public class Action {
	
	private static final int STATE_WAITING = 0; 
	private static final int STATE_STARTED = 1; 
	private static final int STATE_READY   = 2; 
	private static final int STATE_LOCKED  = 3;  
	
	private int state = STATE_WAITING;
	
	private float timeAction;
	private float timeLock;
	
	private float countdown;

	public boolean isReady() {
		if (state == STATE_READY) {
			state = STATE_LOCKED;
			countdown = timeLock;
			return true;
		}
		return false;
	}
	
	public boolean isLocked() {
		return state == STATE_LOCKED;
	}
	
	public boolean isWaiting() {
		return state == STATE_WAITING;
	}
	
	public boolean isStarted() {
		return state == STATE_STARTED;
	}
	
	public void reset() {
		state = STATE_WAITING;
		countdown = 0;
	}
		
	public void start() {
		if (state == STATE_WAITING) {
			countdown = timeAction;
			state = STATE_STARTED;
		}
	}
	
	public void abort() {
		if (state == STATE_STARTED || state == STATE_READY) {
			state = STATE_WAITING;
		}
	}
	
	public float getTimeElapsed() {
		if (state == STATE_STARTED) {
			return (timeAction - countdown) / timeAction;
		}
		if (state == STATE_LOCKED) {
			return (timeLock - countdown) / timeLock;
		}
		return 0;
	}
	
	
	public void update(float tpf) {
		if (STATE_WAITING == state || STATE_READY == state) {
			return;
		}
		countdown -= tpf;
		if (STATE_LOCKED == state) {
			if (countdown < 0) {
				countdown = 0;
				state = STATE_WAITING;
			}
		}
		if (STATE_STARTED == state) {
			if (countdown < 0) {
				countdown = 0;
				state = STATE_READY;
			}
		}
	}

	public float getTimeAction() {
		return timeAction;
	}

	public void setTimeAction(float timeAction) {
		this.timeAction = timeAction;
	}

	public float getTimeLock() {
		return timeLock;
	}

	public void setTimeLock(float timeLock) {
		this.timeLock = timeLock;
	}

	public String getState() {
		switch (state) {
			case STATE_WAITING: return "waiting";
			case STATE_STARTED: return "started";
			case STATE_READY:   return "ready";
			case STATE_LOCKED:  return "locked";
		}
		return null;
	}

	@Override
	public String toString() {
		return "Action [status=" + getState() + ", countdown=" + countdown + "]";
	}
	
	

}

